package com.ujiuye.controller;


import com.ujiuye.config.*;
import com.ujiuye.service.IOrderService;
import com.ujiuye.vo.BusinessVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单-会员下单预约检查 前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
   private IOrderService orderService;


    @PostMapping("/orderDayList")
    public PageResult todayOrderList(@RequestBody QueryPageBean queryPageBean)  {
        try {
            return orderService.todayOrderList(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @PostMapping("/orderWeekList")
    public PageResult toWeekOrderList(@RequestBody QueryPageBean queryPageBean)  {
        try {
            return orderService.toWeekOrderList(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/orderMonthList")
    public PageResult toMonthOrderList(@RequestBody QueryPageBean queryPageBean)  {
        try {
            return orderService.toMothOrderList(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }






     @GetMapping("/business")
    public Result getBusiness(){
         try {
             BusinessVo businessVo = orderService.getBusinessVo();
             return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,businessVo);
         } catch (Exception e) {
             e.printStackTrace();
             return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
         }
     }

    @Value("${upload}")
    private String upload;
    @GetMapping("/businessExcel")
   public ResponseEntity<byte[]> businessExcel() throws IOException {
        BusinessVo businessVo = orderService.getBusinessVo();
        FileInputStream fileInputStream = new FileInputStream(upload+"order.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheetAt = workbook.getSheetAt(0);

        XSSFRow row3 = sheetAt.getRow(2);
        row3.getCell(1).setCellValue(businessVo.getTodayNewMember());
        row3.getCell(3).setCellValue(businessVo.getTotalMember());

        XSSFRow row4 = sheetAt.getRow(3);
        row4.getCell(1).setCellValue(businessVo.getThisWeekNewMember());
        row3.getCell(3).setCellValue(businessVo.getThisMonthNewMember());

        XSSFRow row5 = sheetAt.getRow(5);
        row5.getCell(1).setCellValue(businessVo.getTodayOrderNumber());
        row5.getCell(3).setCellValue(businessVo.getTodayVisitsNumber());

        XSSFRow row6 = sheetAt.getRow(6);
        row6.getCell(1).setCellValue(businessVo.getThisWeekOrderNumber());
        row6.getCell(3).setCellValue(businessVo.getThisWeekVisitsNumber());

        XSSFRow row7 = sheetAt.getRow(7);
        row7.getCell(1).setCellValue(businessVo.getThisMonthOrderNumber());
        row7.getCell(3).setCellValue(businessVo.getThisMonthVisitsNumber());

        List<Map<String, Object>> hotSetmeal = businessVo.getHotSetmeal();

        for (int i=0;i<hotSetmeal.size();i++){
            Map<String, Object> map = hotSetmeal.get(i);
            XSSFRow row = sheetAt.getRow(i + 10);
            row.getCell(0).setCellValue((String)map.get("name"));
            row.getCell(2).setCellValue((long)map.get("setmeal_count"));
            double doubleValue = ((BigDecimal) map.get("proportion")).doubleValue();
            row.getCell(3).setCellValue(doubleValue);
        }
        FileOutputStream fileOutputStream = new FileOutputStream(upload+"order.xlsx");
        workbook.write(fileOutputStream);

        return DownLoad.download(new File(upload+"order.xlsx"));

    }






}

